<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

    $baseUrl = $request->getApplicationPath();
?>
<h1><?php echo $title; ?></h1>
<?php if ($hasCreatePermission): ?>
<form action="<?php echo $baseUrl; ?>/pages/create/" method="get">
    <div class="create-action">
        <button type="submit"><span><?php echo Translator::translate('Create new page'); ?></span></button>
    </div>
</form>
<?php endif; ?>

<?php if ($rootPage != null): ?>
<div class="container" id="page-tree-container">
    <ul>
<?php
    function displayTree($pages, $pageTreeMeta, $level, &$i, $baseUrl, $hasCreatePermission, $hasEditPermission, $hasDeletePermission) {
        $level++;
        foreach ($pages as $page):
            $childrenStatus = isset($pageTreeMeta[$page->id]) ? $pageTreeMeta[$page->id] : null;
            ?>

        <li class="level<?php echo $level; ?> <?php echo ($i++ %2) ? 'even' : 'odd'; ?><?php if ($childrenStatus !== null): ?> has-children<?php endif; ?>">
            <div>
                <h3 class="title">
                    <?php if ($hasEditPermission): ?>
                    <a href="<?php echo $baseUrl; ?>/pages/edit/<?php echo $page->id; ?>/"><?php echo $page->title; ?></a>
                    <?php else: ?>
                    <?php echo $page->title; ?>
                    <?php endif; ?>
                </h3>
                <!--span class="status">

                    (<?php
                        if($page->status == 1):
                            echo Translator::translate('Hidden');
                        elseif($page->status == 2):
                            echo Translator::translate('Preview');
                        elseif($page->status == 3):
                            echo Translator::translate('Published');
                        endif;
                    ?>)
                </span-->
                <div class="actions">
                    <?php if ($hasCreatePermission): ?>
                    <a class="action create-child" href="<?php echo $baseUrl; ?>/pages/create/<?php echo $page->id; ?>/"><?php echo Translator::translate('Create child'); ?></a>
                    <?php endif; ?>
                    <?php if ($hasDeletePermission): ?>
                    <a class="action delete" href="<?php echo $baseUrl; ?>/pages/delete/<?php echo $page->id; ?>/"><?php echo Translator::translate('Delete'); ?></a>
                    <?php endif; ?>
                <?php if ($level > 1): ?>
                    <?php if ($childrenStatus === false): ?>
                    <a class="action expand-children" title="<?php echo Translator::translate("Expand children"); ?>" href="<?php echo $baseUrl; ?>/pages/expand/<?php echo $page->id; ?>"><?php echo Translator::translate("Expand children"); ?></a>
                    <?php elseif ($childrenStatus === true): ?>
                    <a class="action contract-children" title="<?php echo Translator::translate("Contract children"); ?>" href="<?php echo $baseUrl; ?>/pages/contract/<?php echo $page->id; ?>"><?php echo Translator::translate("Contract children"); ?></a>
                    <?php endif; ?>
                <?php endif; ?>
                </div>
            </div>
        <?php if ($page->children !== null): ?>
            <ul>
            <?php displayTree($page->children, $pageTreeMeta, $level, $i, $baseUrl, $hasCreatePermission, $hasEditPermission, $hasDeletePermission); ?>
            </ul>
        <?php endif; ?>
        </li>

    <?php endforeach;
    }
    $i = 0;
    displayTree(array($rootPage), $pageTreeMeta, 0, $i, $baseUrl, $hasCreatePermission, $hasEditPermission, $hasDeletePermission); ?>
    </ul>
</div>
<?php else: ?>
<div class="container">
    <p><?php echo Translator::translate('There are no pages for this site.'); ?></p>
</div>
<?php endif; ?>

